<?php
if ($_POST) {
    ?>
    <div class="alert alert-danger"><i class="icon-exclamation-sign"></i> คุณใส่ข้อมูลไม่ครบถ้วนหรือผิดตามเงื่อนไขกรุณาตรวจสอบฟิลด์ที่กรอบเป็นสีแดง</div>
    <?php
}
?>

<div class="row-fluid headline-row">
    <div class="span12 headline-line">
        <div class="headline pull-left">
            <h1><i class="w-glyphicon-circle-plus"></i> Edit your store</h1>
        </div>
    </div>
    <div class="clearfix"></div>
</div>

<form method="post" action="<?php echo base_url() ?>shop/<?php echo hashids_encrypt($shop['id']) ?>/edit/" class="form-horizontal" style="margin-top:20px;">

    <fieldset>
        <legend>รายละเอียดร้านค้า</legend>
        <div class="control-group <?php echo form_error('shop_title') ? 'error' : '' ?>">
            <label class="control-label">ชื่อร้านค้า</label>
            <div class="controls">
                <input type="text" name="shop_title" placeholder="ชื่อร้านค้าภาษาไทยหรือภาษาอังกฤษ" value="<?php echo set_value('shop_title', $shop['shop_title']) ?>" class="shop_title input-xxlarge">
            </div>
            <script>
                $(function() {
                    /*Do upload cover*/
                    if ($('.shop_title').val() !== '')
                    {
                        $('.shop_editor h2').html($('.shop_title').val());
                    }

                    $('.shop_title').keyup(function() {
                        $('.shop_editor h2').html($('.shop_title').val());
                    });
                });
            </script>
        </div>
        <div class="control-group <?php echo form_error('shop_description') ? 'error' : '' ?>">
            <label class="control-label">รายละเอียดร้านค้า</label>
            <div class="controls">
                <div style="width:544px;">
                    <script>
                        function wrapText(elementID, openTag, closeTag) {
                            var textArea = $('#' + elementID);
                            var len = textArea.val().length;
                            var start = textArea[0].selectionStart;
                            var end = textArea[0].selectionEnd;
                            var selectedText = textArea.val().substring(start, end);
                            var replacement = openTag + selectedText + closeTag;
                            textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
                        }
                    </script>
                    <a class="btn" onclick='wrapText("shop_description", "[b]", "[/b]");'><b>b</b></a>
                    <a class="btn" onclick='wrapText("shop_description", "[i]", "[/i]");'><i>i</i></a>
                    <a class="btn" onclick='wrapText("shop_description", "[u]", "[/u]");'><u>u</u></a>

                    <textarea id="shop_description" style="height:200px;resize:none" name="shop_description" placeholder="ควรใส่รายละเอียดให้ครบถ้วนว่าขายอะไรและแนวทางการขายคืออะไรรวมถึงการติดต่อเช่น เบอร์โทร,Line ID,BB PIN,ที่ตั้งร้าน เป็นต้น (คุณสามารถใส่รูปเพิ่มเติมจากปุ่ม Select Image ด้านล่าง)" class="input-xxlarge"><?php echo set_value('shop_description', $shop['shop_description']) ?></textarea>
                    <div style="margin-top:10px;">
                        <script type="text/javascript" src="http://upic.me/js/embedupload.js"></script>
                        <script type="text/javascript">
                        upic_buttoncss += "color:#000000;";
                        function upic_custom(urlshow, urlfull, urlthumb) {
                            $('#shop_description').val($('#shop_description').val() + '[img]' + urlfull + '[/img]');
                        }
                        </script>
                    </div>

                </div>
            </div>
        </div>
        <div class="control-group <?php echo form_error('shop_category') ? 'error' : '' ?>">
            <label class="control-label">หมวดร้านค้า</label>
            <div class="controls">
                <div class="btn-group" data-toggle="buttons-radio">
                    <?php
                    $default_category = set_value('shop_category', $shop['shop_category']);
                    ?>
                    <button type="button" class="category btn btn-primary <?php echo $default_category == 'women' ? 'active' : '' ?>" value="women" title="คุณผู้หญิง"><i class="w-glyphicon-woman"></i> Women</button>
                    <button type="button" class="category btn btn-primary <?php echo $default_category == 'men' ? 'active' : '' ?>" value="men" title="คุณผู้ชาย"><i class="w-glyphicon-user"></i> Men</button>
                    <button type="button" class="category btn btn-primary <?php echo $default_category == 'kids' ? 'active' : '' ?>" value="kids"  title="คุณหนู"><i class="w-glyphicon-girl"></i> Kids</button>
                    <button type="button" class="category btn btn-primary <?php echo $default_category == 'pets' ? 'active' : '' ?>" value="pets"  title="สัตว์เลี้ยง"><i class="w-glyphicon-dog"></i> Pets</button>
                    <button type="button" class="category btn btn-primary <?php echo $default_category == 'mobiles-it' ? 'active' : '' ?>" value="mobiles-it"  title="มือถือและไอที"><i class="w-glyphicon-iphone"></i> Mobiles & IT</button>
                    <button type="button" class="category btn btn-primary <?php echo $default_category == 'gadgets-hobbies' ? 'active' : '' ?>" value="gadgets-hobbies"  title="ของเล่นและงานอดิเรก"><i class="w-glyphicon-gamepad"></i> Gadgets & Hobbies</button>
                </div>
                <input type="hidden" class="shop_category" name="shop_category" class="input-xxlarge" value="<?php echo $default_category ?>">
                <script>
                    /*Check Default category*/
                    $('.shop_category').val($('.category.active').val());
                    $('.category').click(function() {
                        $('.shop_category').val($(this).val());
                    });
                </script>
            </div>
        </div>
    </fieldset>

    <fieldset>
        <legend>การเข้าถึงร้านในโลกออนไลน์</legend>
        <div class="control-group <?php echo form_error('shop_link') ? 'error' : '' ?>">
            <label class="control-label"><img src="<?php echo base_url() ?>img/icons/link.png"/></label>
            <div class="controls">
                <input type="text" name="shop_link" value="<?php echo set_value('shop_link', $shop['shop_link']) ?>" placeholder="http://www.your_shop.com" class="input-xxlarge">
            </div>
        </div>
        <div class="control-group <?php echo form_error('shop_facebook') ? 'error' : '' ?>">
            <label class="control-label"><img src="<?php echo base_url() ?>img/icons/facebook.png"/></label>
            <div class="controls">
                <input type="text" name="shop_facebook" value="<?php echo set_value('shop_facebook', $shop['shop_facebook']) ?>" placeholder="http://www.facebook.com/fanpage_username" class="input-xxlarge">
            </div>
        </div>
        <div class="control-group <?php echo form_error('shop_instagram') ? 'error' : '' ?>">
            <label class="control-label"><img src="<?php echo base_url() ?>img/icons/instagram.png"/></label>
            <div class="controls">
                <input type="text" name="shop_instagram" value="<?php echo set_value('shop_instagram', $shop['shop_instagram']) ?>" placeholder="http://www.instagram.com/instagram_username" class="input-xxlarge">
            </div>
        </div>
    </fieldset>

    <fieldset>
        <legend>ใบปะหน้าในระบบพื้นฐาน</legend>




        <div class="control-group <?php echo form_error('shop_level0_description') ? 'error' : '' ?>">
            <label class="control-label">ใบปะหน้าระดับ 1</label>
            <div class="controls">
                <div class="alert"><i class="icon-exclamation-sign"></i> ใบปะหน้านี้คือใบปะหน้าเริ่มต้นของระบบ</div>
                <div class="masonry-container shop_editor">
                    <?php echo $this->load->view('shop_level/level0') ?>
                    <div class="clearfix"></div>
                </div>

                <div id="shop_level0_upload_container" >
                    <a <?php echo form_error('shop_level0_cover') ? 'style="border:1px solid red"' : '' ?> id="shop_level0_pickfiles" href="javascript:void(0)" class="btn btn-primary" data-loading-text="กำลังอัพโหลด..."><i class="icon-upload"></i> เปลี่ยนภาพหน้าปก</a> 
                    <input type="hidden" name="shop_level0_cover" class="shop_level0_cover" value="<?php echo set_value('shop_level0_cover', $shop['shop_level0_cover']) ?>" />
                </div>

                <br/>
                <script>
                    $(function() {

                        /*Set fail form*/
                        if ($('.shop_level0_cover').val() !== '')
                        {
<?php if (isset($_POST['shop_level0_cover']) && $_POST['shop_level0_cover'] != $shop['shop_level0_cover']): ?>
                                $('.level0-container img').attr('src', '<?php echo base_url() ?>uploads/tmp_cover/<?php echo $tmp_timestamp ?>/' + $('.shop_level0_cover').val());
<?php else: ?>
                                $('.level0-container img').attr('src', '<?php echo base_url() ?>uploads/shops/<?php echo crc32_hash($shop['id']) ?>/<?php echo $shop['id'] ?>/' + $('.shop_level0_cover').val());
<?php endif; ?>
                        }
                        if ($('.shop_level0_description').val() !== '')
                        {
                            $('.shop_editor .level0 .description p').html($('.shop_level0_description').val());
                        }


                        $('.shop_level0_description').keyup(function() {
                            $('.shop_editor .level0 .description p').html($('.shop_level0_description').val());
                        });


                        var current_filename;
                        var shop_level0_upload = new plupload.Uploader({
                            runtimes: 'html5,flash,html4',
                            browse_button: 'shop_level0_pickfiles',
                            container: 'shop_level0_upload_container',
                            max_file_size: '2mb',
                            multi_selection: false,
                            url: '<?php echo base_url() ?>shop/tmp_cover_upload',
                            flash_swf_url: '<?php echo base_url() ?>js/plupload.flash.swf',
                            multipart_params: {
                                "tmp_timestamp": "<?php echo $tmp_timestamp ?>",
                                "level": "0"
                            },
                            filters: [
                                {title: "Image files", extensions: "jpg,gif,png"},
                            ],
                        });
                        shop_level0_upload.init();
                        shop_level0_upload.bind('FilesAdded', function(up, files) {
                            current_filename = files[0].name;
                            $('#shop_level0_pickfiles').button('loading');
                            shop_level0_upload.start();
                        });
                        shop_level0_upload.bind('FileUploaded', function(up, files, info) {
                            var info = jQuery.parseJSON(info.response);

                            if (typeof info.error == 'undefined') {

                                $('.shop_level0_cover').val(info.result);

                                $('.level0-container img').attr('src', '<?php echo base_url() ?>uploads/tmp_cover/<?php echo $tmp_timestamp ?>/' + info.result);
                                $('#shop_level0_pickfiles').button('reset');
                            }
                        });
                    });
                </script>

                <input type="text" value="<?php echo set_value('shop_level0_description', $shop['shop_level0_description']) ?>" name="shop_level0_description" placeholder="คำทักทายใบปะหน้าระดับ 1" class="shop_level0_description input-xxlarge">
            </div>
        </div>
        <hr/>
        <div class="control-group <?php echo form_error('shop_level1_description') ? 'error' : '' ?>">
            <label class="control-label">ใบปะหน้าระดับ 2</label>
            <div class="controls">
                <div class="alert"><i class="icon-exclamation-sign"></i> ใบปะหน้านี้จะถูกใช้เมื่อมียอด "โหวตชอบ" ในระบบสูงกว่าค่าเฉลี่ย "โหวตชอบ" ในระบบ</div>
                <div class="masonry-container shop_editor">
                    <?php echo $this->load->view('shop_level/level1') ?>
                    <div class="clearfix"></div>
                </div>

                <div id="shop_level1_upload_container">
                    <a <?php echo form_error('shop_level1_cover') ? 'style="border:1px solid red"' : '' ?> id="shop_level1_pickfiles" href="javascript:void(0)" class="btn btn-primary" data-loading-text="กำลังอัพโหลด..."><i class="icon-upload"></i> เปลี่ยนภาพหน้าปก</a> 
                    <input type="hidden" name="shop_level1_cover" class="shop_level1_cover" value="<?php echo set_value('shop_level1_cover', $shop['shop_level1_cover']) ?>" />
                </div>

                <br/>
                <script>
                    $(function() {
                        /*Set fail form*/
                        if ($('.shop_level1_cover').val() !== '')
                        {

<?php if (isset($_POST['shop_level1_cover']) && $_POST['shop_level1_cover'] != $shop['shop_level1_cover']): ?>
                                $('.level1-container img').attr('src', '<?php echo base_url() ?>uploads/tmp_cover/<?php echo $tmp_timestamp ?>/' + $('.shop_level1_cover').val());
<?php else: ?>
                                $('.level1-container img').attr('src', '<?php echo base_url() ?>uploads/shops/<?php echo crc32_hash($shop['id']) ?>/<?php echo $shop['id'] ?>/' + $('.shop_level1_cover').val());
<?php endif; ?>



                        }
                        if ($('.shop_level1_description').val() !== '')
                        {
                            $('.shop_editor .level1 .description p').html($('.shop_level1_description').val());
                        }


                        $('.shop_level1_description').keyup(function() {
                            $('.shop_editor .level1 .description p').html($('.shop_level1_description').val());
                        });

                        var current_filename;
                        var shop_level1_upload = new plupload.Uploader({
                            runtimes: 'html5,flash,html4',
                            browse_button: 'shop_level1_pickfiles',
                            container: 'shop_level1_upload_container',
                            max_file_size: '2mb',
                            multi_selection: false,
                            url: '<?php echo base_url() ?>shop/tmp_cover_upload',
                            flash_swf_url: '<?php echo base_url() ?>js/plupload.flash.swf',
                            multipart_params: {
                                "tmp_timestamp": "<?php echo $tmp_timestamp ?>",
                                "level": "1"
                            },
                            filters: [
                                {title: "Image files", extensions: "jpg,gif,png"},
                            ],
                        });
                        shop_level1_upload.init();
                        shop_level1_upload.bind('FilesAdded', function(up, files) {
                            current_filename = files[0].name;
                            $('#shop_level1_pickfiles').button('loading');
                            shop_level1_upload.start();
                        });
                        shop_level1_upload.bind('FileUploaded', function(up, files, info) {
                            var info = jQuery.parseJSON(info.response);
                            if (typeof info.error == 'undefined') {
                                $('.shop_level1_cover').val(info.result);
                                $('.level1-container img').attr('src', '<?php echo base_url() ?>uploads/tmp_cover/<?php echo $tmp_timestamp ?>/' + info.result);
                                $('#shop_level1_pickfiles').button('reset');
                            }
                        });
                    });
                </script>

                <input type="text" value="<?php echo set_value('shop_level1_description', $shop['shop_level1_description']) ?>" name="shop_level1_description" placeholder="คำทักทายใบปะหน้าระดับ 2" class="shop_level1_description input-xxlarge">
            </div>
        </div>

        <hr/>
        <div class="control-group <?php echo form_error('shop_color') ? 'error' : '' ?>">
            <label class="control-label">สีของใบปะหน้า</label>
            <div class="controls">
                <?php
                $default_color = set_value('shop_color', $shop['shop_color']);
                ?>
                <div class="btn-group" data-toggle="buttons-radio">
                    <button type="button" class="color btn grey <?php echo $default_color == 'grey' ? 'active' : '' ?>" value="grey">GREY</button>
                    <button type="button" class="color btn black <?php echo $default_color == 'black' ? 'active' : '' ?>" value="black">BLACK</button>
                    <button type="button" class="color btn purple <?php echo $default_color == 'purple' ? 'active' : '' ?>" value="purple">PURPLE</button>
                    <button type="button" class="color btn magenta <?php echo $default_color == 'magenta' ? 'active' : '' ?>" value="magenta">MAGENTA</button>
                    <button type="button" class="color btn teal <?php echo $default_color == 'teal' ? 'active' : '' ?>" value="teal">TEAL</button>
                    <button type="button" class="color btn lime <?php echo $default_color == 'lime' ? 'active' : '' ?>" value="lime">LIME</button><br/><br/>
                    <button type="button" class="color btn brown <?php echo $default_color == 'brown' ? 'active' : '' ?>" value="brown">BROWN</button>
                    <button type="button" class="color btn pink <?php echo $default_color == 'pink' ? 'active' : '' ?>" value="pink">PINK</button>
                    <button type="button" class="color btn orange <?php echo $default_color == 'orange' ? 'active' : '' ?>" value="orange">ORANGE</button>
                    <button type="button" class="color btn blue <?php echo $default_color == 'blue' ? 'active' : '' ?>" value="blue">BLUE</button>
                    <button type="button" class="color btn red <?php echo $default_color == 'red' ? 'active' : '' ?>" value="red">RED</button>
                    <button type="button" class="color btn green <?php echo $default_color == 'green' ? 'active' : '' ?>" value="green">GREEN</button>

                </div>
                <input type="hidden" class="shop_color" name="shop_color" class="input-xxlarge" value="<?php echo $default_color ?>">
                <script>
                    /*Check Default category*/
                    $(function() {
                        $('.level0,.level1').addClass($('.shop_color').val());
                        $('.color').click(function() {
                            $('.level0,.level1').removeClass('grey');
                            $('.level0,.level1').removeClass('black');
                            $('.level0,.level1').removeClass('purple');
                            $('.level0,.level1').removeClass('magenta');
                            $('.level0,.level1').removeClass('teal');
                            $('.level0,.level1').removeClass('lime');
                            $('.level0,.level1').removeClass('brown');
                            $('.level0,.level1').removeClass('pink');
                            $('.level0,.level1').removeClass('orange');
                            $('.level0,.level1').removeClass('blue');
                            $('.level0,.level1').removeClass('red');
                            $('.level0,.level1').removeClass('green');

                            $('.level0,.level1').addClass($(this).val());
                            $('.shop_color').val($(this).val());
                        });
                    });
                </script>
            </div>
        </div>

    </fieldset>

    <fieldset>
        <legend>อีเมล์สำหรับการแจ้งเตือนจากระบบและรหัสผ่านสำหรับการแก้ไขการประกาศร้านค้า</legend>
        <div class="control-group <?php echo form_error('shop_email') ? 'error' : '' ?>">
            <label class="control-label">เปลี่ยนอีเมล์รับการแจ้งเตือน</label>
            <div class="controls">
                <input type="text" name="shop_email" value="<?php echo set_value('shop_email', $shop['shop_email']) ?>" placeholder="john-doe@email.com" class="input-xxlarge">
            </div>
        </div>
        <div class="control-group <?php echo form_error('shop_password') ? 'error' : '' ?>">
            <label class="control-label">เปลี่ยนรหัสผ่านร้านค้า</label>
            <div class="controls">
                <input type="text" name="shop_password" value="<?php echo set_value('shop_password') ?>" placeholder="ต้องไม่ต่ำว่า 6 ตัวอักษรและควรประกอบด้วยตัวเลข" class="input-xxlarge">
            </div>
        </div>
    </fieldset>

    <fieldset>
        <legend>ยืนยันการเปลี่ยนข้อมูล</legend>
        <div class="control-group <?php echo form_error('confirm_password') ? 'error' : '' ?>">
            <label class="control-label">รหัสผ่านสำหรับเปลี่ยนข้อมูล</label>
            <div class="controls">
                <input type="password" name="confirm_password" value="" placeholder="รหัสของร้านเพื่อยืนยันการแก้ไข" class="input-xxlarge">
            </div>
        </div>
    </fieldset>


    <input type="hidden" name="tmp_timestamp" value="<?php echo $tmp_timestamp ?>"/>
    <hr/>
    <div class="control-group">
        <div class="controls">
            <button type="submit" class="btn btn-success"><i class="icon-edit"></i> แก้ไขร้าน</button>
            <a href="<?php echo base_url().'shop/'.hashids_encrypt($shop['id']) ?>" class="btn btn-danger">ย้อนกลับไปที่ร้าน</a>
        </div>
    </div>

</form>